import React from 'react';
import {
  Card, //Divider,
  Button
} from 'antd';

import DescriptionList from '@/components/DescriptionList';

import ODOO from 'odoojs-finance';

//const host = 'http://www.bjssrs.cn:8069';
const host = 'http://192.168.56.101:8069';
const db = 'T3';

const success = ({url, params, result})=>{
    console.log('rpc call url:', url);
    console.log('rpc call params:', params);
    console.log('rpc call result:',  result);
};
const error = ({url, params, error}) => {
    console.log('rpc call url:', url);
    console.log('rpc call params:', params);
    console.log('rpc call error:',  Object.keys( error) );
    console.log('rpc call error:',  error);

    Object.keys(error).forEach(item=>{
        console.log( item, error[item] );
    })
};

const odoo = ODOO({ host, db, success, error} );


const { Description } = DescriptionList;

class List extends React.Component {
  state = {
    userName: 'admin@comp1',
    password: '123',
    ret: null,
    error: null,
  }

  componentDidMount() {
    const success = this.success;
    const error = this.error;
    odoo.setCallback({ success, error} );
  }

  success = ({url, params, result})=>{
    console.log('rpc call url:', url);
    console.log('rpc call params:', params);
    console.log('rpc call result:',  result);

    this.setState({  error: {} })
  }

  error = ({url, params, error})=>{
    console.log('rpc call url:', url);
    console.log('rpc call params:', params);
    console.log('rpc call error:',  Object.keys( error) );
    console.log('rpc call error:',  error);

    Object.keys(error).forEach(item=>{
        console.log( item, error[item] );
    })

    this.setState({  error: {url, params, error} })
  }

  async login() {
    const {userName, password} = this.state;
    const ret = await odoo.login({login: userName, password });
    this.setState({  ret });
  }

  async login2() {
    const {userName} = this.state;
    const ret = await odoo.login({login: userName, password: '111' });
    this.setState({  ret })
  }

  async logout() {
    const ret = await odoo.logout();
    this.setState({  ret })
  }

  async api() {
    const domain = [['name','like','%']]
    const partner = await odoo.env['res.partner'].search( domain );
    const ret = partner.look2();
    this.setState({  ret });
  }

  async api2() {
    const partner = await odoo.env['res.partner'].search( [1,2,3] )
    const ret = partner.look2()
    this.setState({  ret })
  }


  render() {
    const { userName, password, ret,error } = this.state;

    return (
      <div>
        <Card bordered={false}>
          <DescriptionList size="large" title="login" style={{ marginBottom: 32 }}>
            <Button onClick={()=>this.login()}>登录</Button>
            <Button onClick={()=>this.login2()}>登录密码错</Button>
            <Button onClick={()=>this.logout()}>登出</Button>
            <Button onClick={()=>this.api()}>api</Button>
            <Button onClick={()=>this.api2()}>api wrong</Button>
          </DescriptionList>

          <DescriptionList size="large" title="初始化" style={{ marginBottom: 32 }}>
            <Description term="服务器地址">{host}</Description>
          </DescriptionList>

          <DescriptionList size="large" title="" style={{ marginBottom: 32 }}>
            <Description term="数据库名">{db}</Description>
          </DescriptionList>

          <DescriptionList size="large" title="登录账号" style={{ marginBottom: 32 }}>
            <Description term="login">{userName}</Description>
            <Description term="password">{password}</Description>
          </DescriptionList>

          <DescriptionList size="large" title="返回结果" style={{ marginBottom: 32 }}>
            <Description term="return">{JSON.stringify(ret)}</Description>
          </DescriptionList>

          <DescriptionList size="large" title="" style={{ marginBottom: 32 }}>
            <Description term="error">{JSON.stringify(error)}</Description>
          </DescriptionList>


        </Card>


      </div>
    );
  }
}

export default List;





